const config = require('../util/config')
const chromeMng = require('../mng/chromeMng')
const formatDate = require('../../lib/formatDate')
const logger = require('log4js').getLogger()
const taobaoClient = require('../mng/taobaoClientMng').client
const fanliTool = require('../util/fanliTool')
const Wechat = require('../util/Wechat')

module.exports = (req, res) => {
    require('../util/service').handlePost(req, res, async(reqObj, resolve) => {
        if (!reqObj.link) {
            logger.warn('[' + __filename + ':' + __line + ']', 'lack link')
            resolve({
                result: -1,
                msg: '缺少link'
            })
            return
        }
        if (config.domain == '') {
            if (reqObj.link.indexOf('jd.com') != -1) {
                resolve({
                    result: -1,
                    msg: '该商品不能返利'
                })
                return
            }
            chromeMng[reqObj.link.indexOf('jd.com') == -1 ? 'getTaobaoAdvLink' : 'getJdAdvLink']({
                link: reqObj.link
            }).then(result => {
                resolve(result)
            }, result => {
                resolve({
                    result: -1,
                    msg: result
                })
            })
        } else if (config.domain == 'zoro.site') {
            fanliTool.getAdvLink(reqObj.link, {
                customer_phone: req.session.customer.phone
            }).then(item => {
                //logger.debug('[' + __filename + ':' + __line + ']', item)
                resolve(item)
            }, result => {
                var content = '获取' + reqObj.link + '的推广链接失败'
                require('../mng/pushMng').push(content)
                resolve({
                    result: -1,
                    msg: '系统错误'
                })
            })
        }
    })
}